package com.zjff.show.zjffshow.controller;

import com.zjff.show.zjffshow.entity.PrevenientDao;
import com.zjff.show.zjffshow.entity.ResultDao;
import com.zjff.show.zjffshow.entity.UserDao;
import com.zjff.show.zjffshow.service.PrevenientService;
import com.zjff.show.zjffshow.service.UserService;
import com.zjff.show.zjffshow.tool.JSONTool;
import com.zjff.show.zjffshow.tool.RedisTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Prevenient/")
public class PrevenientController {

    @Autowired
    PrevenientService prevenientService;
    ResultDao resultDao = null;
    Jedis jedis = new RedisTool().getJedis();


    @RequestMapping(value = "/getPrevenientList",method = RequestMethod.GET)
    private String getPrevenientList(HttpServletRequest request){
        resultDao = new ResultDao();
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        String id = request.getParameter("id");
        String Pwd = request.getParameter("Pwd");
        String Phone = request.getParameter("Phone");
        String Address = request.getParameter("Address");
        int intid = 0;
        boolean flag = false;
        if (id!=null && !id.equals("") && !id.equals("0")){
            intid = Integer.parseInt(id);
            flag = true;
        }
        if (!Pwd.equals("")){
            flag = true;
        }
        if (!Phone.equals("")){
            flag = true;
        }
        if (!Address.equals("")){
            flag = true;
        }
        int Start=(page-1)*limit;//设置起始位置
        List<PrevenientDao> list = prevenientService.getPrevenientList(intid,Pwd,Start,limit,Phone,Address);
        resultDao.setCode("0");
        resultDao.setMsg("获取成功！");
        if (flag){
            resultDao.setCount(String.valueOf(list.size()));
        }else {
            resultDao.setCount(String.valueOf(prevenientService.getPrevenientCount()));
        }
        resultDao.setData(list);
        return JSONTool.getJsonTool().toJson(resultDao);
    }

    //获取所有用户数量
    @RequestMapping(value = "/getPrevenientCount",method = RequestMethod.GET)
    private String getUserCount(HttpServletRequest request){
        resultDao = new ResultDao();
        int count = prevenientService.getPrevenientCount();
        resultDao.setCount(String.valueOf(count));
        return JSONTool.getJsonTool().toJson(resultDao);
    }
    @RequestMapping(value = "/addPrevenient",method = RequestMethod.GET)
    private String addPrevenient(HttpServletRequest request){
        resultDao = new ResultDao();
        PrevenientDao prevenientDao = new PrevenientDao();
        prevenientDao.setPwd(request.getParameter("Pwd"));
        prevenientDao.setPhone(request.getParameter("Phone"));
        prevenientDao.setAddress(request.getParameter("Address"));
        int result = prevenientService.addPrevenient(prevenientDao);
        if (result>0){
            resultDao.setCode("0");
            resultDao.setMsg("添加成功！");
        }else {
            resultDao.setCode("1");
            resultDao.setMsg("添加失败！");
        }
        return JSONTool.getJsonTool().toJson(resultDao);
    }


}
